/**
 * @class Prototype.Compte
 */ 
var Compte = Class.create(AjaxClient,
{
  /**
   * initialize
   */
  initialize: function()
  {
    this.setServer('compte.ajaxsrv.php');
    
    $('div_new_op').hide();
    
    this.wndOperation = new Window( { maximizable: false } );
    this.wndOperation.setContent('div_new_op', true, false);
    
    TooltipManager.options.shiftX =  0;
    TooltipManager.options.shiftY = 40;
    
    this.action = 'add_op'    
    this.ajaxUpdate('operations', { action : 'load' });
    this.ajaxUpdate('solde', { action : 'get_solde' });
    this.ajaxUpdate('bilan_categories', { action : 'get_bilan_categories' });
    this.ajaxUpdate('select_categories', { action : 'get_list_categories' });
    this.registerCallbacks();
  },
  
  /**
   * registerCallbacks
   */
  registerCallbacks: function()
  {
    // NEW OPERATION VISIBILITY
    Event.observe('btn_show_debit', 'click', function(event)
    {
      this.showWndCompte('', 0, '', 'debit', '', '');
      Event.stop(event);
    }.bind(this));

    Event.observe('btn_show_credit', 'click', function(event)
    {
      this.showWndCompte('', 0, '', 'credit', '', '');
      Event.stop(event);
    }.bind(this));
  
    // CLICK DATEPICKER DATE
    Event.observe('btn_dp_date', 'click', function(event)
    {
      displayDatePicker('input_date', true);
    }.bind(this));

    // NEW CATEGORIE
    Event.observe('btn_add_categorie', 'click', function(event)
    {
      this.showEditCategorie(true);
      Event.stop(event);
    }.bind(this));
    
    // HIDE NEW CATEGORIE
    Event.observe('btn_close_categories', 'click', function(event)
    {
      this.showEditCategorie(false);
      Event.stop(event);
    }.bind(this));
    
    // NEW CATEGORIE SUBMIT
    Event.observe('btn_new_cat', 'click', function(event)
    {
      var params = { action : 'new_cat'
                   , nom    : $F('input_newcat')
                   };
      this.ajaxUpdate('select_categories', params, Insertion.Bottom);
      this.showEditCategorie(false);
      Event.stop(event);
    }.bind(this));
    
    // NEW OPERATION SUBMIT
    Event.observe('btn_submit_operation', 'click', function(event)
    {
        var params = { action  : this.action
                     , date    : $F('input_date')
                     , montant : $F('input_montant')
                     , id_cat  : $F('select_categories')
                     , type    : $F('type')
                     , infos   : $F('txt_infos')
                     , id_op   : $F('id_op')
                     };
      if (this.action == 'add_op')
        this.ajaxUpdate('operations', params, Insertion.Bottom);
      else if (this.action == 'update_op')
      {
        var id_row = 'op_' + $F('id_op'); 
        this.ajaxUpdate(id_row, params);      
      }
      this.hideWindow();
      Event.stop(event);
    }.bind(this));
  },
  
  /**
   * showEditCategorie
   */
  showEditCategorie: function(bShow)
  {
    $('select_categories', 'btn_add_categorie').invoke(!bShow ? 'show' : 'hide');
    $('input_newcat', 'btn_new_cat', 'btn_close_categories').invoke(bShow ? 'show' : 'hide');
    if (bShow)
      Form.Element.focus('input_newcat');
  },

  showWndCompte: function(id, id_cat, date, type, montant, information)
  {
   switch (type)
    {
    case 'credit':
      Form.Element.setValue('type', 'credit');
      this.wndOperation.setTitle('Edition du crédit');
      break;
    case 'debit':
    default:
      Form.Element.setValue('type', 'debit');
      this.wndOperation.setTitle('Edition du débit');
      break;
    }
    this.showEditCategorie(false);
    this.wndOperation.showCenter(true);
    Form.Element.focus('input_date');
    
    if (id == '') 
    {
      this.action = 'add_op';
      $('btn_submit_operation').value = 'Ajouter';
    }
    else
    {
      this.action = 'update_op';
      $('btn_submit_operation').value = 'Edition';
    }
    $('id_op').value =  id;
    Form.Element.setValue('input_date',  date);
    Form.Element.setValue('input_montant',  montant);
    Form.Element.setValue('txt_infos',      information);
    Form.Element.setValue('select_categories',id_cat);
       
  },
  
  /**
   * hideWindow
   */
  hideWindow: function()
  {
    this.wndOperation.hide();
  },
  
  /**
   * look_op : observe les actions sur une ligne d'operation
   */
  look_op: function(id_row, id, id_cat, date, type, montant, information)
  {
    var id_infos = 'infos_'+id;
    var id_btn_edit = 'btn_edit_'+id;
    var id_btn_del =  'btn_del_'+id;
    
    Element.hide(id_infos);
    
    TooltipManager.addHTML(id_row, id_infos);
    
    new Event.Over(id_row);   
     
    
    Event.observe(id_btn_del, 'click', function(event)
    {
      var msg = 'Etes vous sûr de vouloir supprimer l\'opération ?';
      if (confirm(msg))
      {
        this.ajaxRequest( { action : 'delete', id: id } );
      }
      Event.stop(event);
    }.bind(this));

    Event.observe(id_btn_edit, 'click', function(event)
    {
      this.showWndCompte(id, id_cat, date, type, montant, information)
      Event.stop(event);
    }.bind(this));
  },
  
   /**
   * delete_opt
   */
  delete_op: function(id)
  {
    $('op_' + id).remove();
  }       
});

Event.observe(window, "load", function(event)
{
  window.Compte = new Compte();
});
